package com.ease.model.criterion;

import java.util.Arrays;
import java.util.List;

import com.ease.common.vo.Entry;

/**
 * 
 * @author nathanleewei
 * 
 */
public class InExpression extends CriterionSupport implements Criterion {

	/**
	 *
	 */
	private static final long serialVersionUID = -8830563969879164446L;
	private String propertyName;
	private Object[] values;

	public InExpression() {

	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public Object[] getValues() {
		return values;
	}

	public void setValues(Object[] values) {
		this.values = values;
	}

	protected InExpression(String propertyName, Object[] values) {
		this.propertyName = propertyName;
		this.values = values;
	}

	public String toSqlString(QueryCriteria queryCriteria) {

		String columnName = queryCriteria.getColumnNameByPropertyName(propertyName, values[0]);
		StringBuffer fragment = new StringBuffer();
		// fragment.append(columnName).append(" in (:"
		// ).append(propertyName).append(")");
		if (notEmpty(queryCriteria.getSpecialCondition(propertyName))) {
			fragment.append(queryCriteria.getSpecialCondition(propertyName));
		} else {
			fragment.append(columnName).append(" in ( ? )");
		}
		return fragment.toString();
	}

	public List<Entry<String, Object>> getNamedValue(QueryCriteria queryCriteria) {
		// Map<String,Object> parameter=new HashMap<String,Object>();
		// parameter.put(propertyName, Arrays.asList(values));
		// return parameter;
		List<Entry<String, Object>> namedValue = this.safeNamedValue(null);
		// MULTIPLE_DICTIONARY ,need special
		if (notEmpty(queryCriteria.getSpecialCondition(propertyName))) {
			namedValue.add(new Entry<String, Object>(propertyName, queryCriteria.toSpecialValue(propertyName, values)));
			return namedValue;
		}
		namedValue.add(new Entry<String, Object>(propertyName, Arrays.asList(values)));
		return namedValue;
	}

	public String toString() {
		return propertyName + " in (" + (values) + ')';
	}
}
